package com.dude.screens;

import java.util.ArrayList;

import com.dude.R;
import com.dude.interfaces.IMemoryService;
import com.dude.objects.GenericPair;
import com.dude.objects.GenericTriplet;
import com.dude.objects.ImageAndTextArrayAdapter;
import com.dude.utils.FilesOperations;
import com.dude.utils.TTS;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;

/**
 * this is the first screen where the user can select the item he wish to find and start the search
 */
public class FindObjectScreen extends Activity implements TextToSpeech.OnInitListener
{

	// ==================================================
	

	public static final String START_SEARCH_ON_SELECTED_OBJECT = "com.dude.start_search_on_object";
    
	private IMemoryService mem;
	private ArrayList<GenericTriplet<String, Bitmap, Boolean>> objects = null;
	private GridView gridview;
	protected TextToSpeech tts;

	private boolean useVoice;
	
	public static ArrayList<GenericPair<Bitmap, String>> bitmapsForSearch;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.find_it);
		
		bitmapsForSearch = new ArrayList<GenericPair<Bitmap, String>>();
		mem = new FilesOperations(this);
		
		SharedPreferences settings = getSharedPreferences(Settings.settings, 0);
        useVoice = settings.getBoolean(Settings.useVoicePref, false);
        
		if (useVoice && tts == null)
		{
			tts = TTS.getInstance(this, this);
		}
		
		gridview = (GridView) findViewById(R.id.grid_thumbnails_id);

		objects = mem.getAllObjects();
		if (objects == null || objects.isEmpty())
			return;

		ArrayList<GenericPair<String, Bitmap>> o = new ArrayList<GenericPair<String,Bitmap>>(objects.size());
		for (GenericTriplet<String, Bitmap, Boolean> t : objects)
		{
			if (t.getSecond()!=null)
				o.add(new GenericPair<String, Bitmap>(t.getFirst(), t.getSecond()));
		}
		
        gridview.setAdapter(new ImageAndTextArrayAdapter(this, o, true)); 
        gridview.setOnItemClickListener(new OnItemClickListener() { 
        	public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
        	{ 
        		cleanBitmapsFromMemory();
        		ArrayList<GenericPair<Bitmap, String>> bmps = mem.getImagesAnrSoundsForObject(new GenericPair<String, Boolean>(objects.get(position).getFirst(), objects.get(position).getThird()) );
        		bitmapsForSearch.addAll(bmps);

            	Intent intent = new Intent(FindObjectScreen.this, ActiveSearchScreen.class);
            	intent.setAction(START_SEARCH_ON_SELECTED_OBJECT);
            	startActivity(intent);
        	} 
        }); 
                
        gridview.setOnItemLongClickListener(new OnItemLongClickListener(){
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){				
				//get the selected item
	            String profile = objects.get(position).getFirst();
	            if (tts != null)
	            	tts.speak(profile,TextToSpeech.QUEUE_FLUSH,null);
	            
	            return true;
			}			 
		 });
	}
	
	public static void cleanBitmapsFromMemory() 
	{
		if (bitmapsForSearch != null && !bitmapsForSearch.isEmpty())
		{
			for (GenericPair<Bitmap, String> p: FindObjectScreen.bitmapsForSearch)
        	{
        		p.getFirst().recycle();
        		p.setFirst(null);
        	}
        	bitmapsForSearch.clear();
		}
	}

	/**
     * Executed when a new TTS is instantiated. Some static text is spoken via TTS here.
     * @param i
     */
    public void onInit(int i)
    {

    }
   
	
}
